package com.julis.db;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.j256.ormlite.stmt.UpdateBuilder;
import com.julis.bean.Life_UserInfo;
import com.julis.bean.Obu_Info;
import com.util.JSONUtils;


/**
 * Created by luoliwen on 16/4/28.
 * 完成对数据库的CRUD的操作
 */
public class Z_UserInfoDao {
    private Context context;
    private Dao<Life_UserInfo, Integer> userDao;
    private Z_DataBaseHelper helper;

    public Z_UserInfoDao(Context context) {
        this.context = context;
        helper = Z_DataBaseHelper.getInstance(context);
        try {
            userDao = helper.getDao(Life_UserInfo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void addUser(String account, String password
    ) {
        try {
            Life_UserInfo user = new Life_UserInfo();
            user.setAccount(account);
            user.setPassword(password);
            user.setId(UUID.randomUUID().toString().substring(0, 7));
            user.setPermission(map());
            userDao.create(user);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public boolean shifoucunzai(String account) {
        try {
            List<Life_UserInfo> account1 = userDao.queryBuilder().where().eq("account", account).query();
            if (account1.size() > 0) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public String getpassword(String account) {
        try {
            Life_UserInfo account1 = userDao.queryBuilder().where().eq("account", account).query().get(0);
            return account1.getPassword();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String cc() {
        try {
            Life_UserInfo account1 = userDao.queryForAll().get(0);
            return account1.getPassword();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Life_UserInfo> queryForAll() {
        try {
            List<Life_UserInfo> account1 = userDao.queryForAll();
            return account1;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void detelone(String account) {
        try {
            Life_UserInfo account1 = userDao.queryBuilder().where().eq("account", account).query().get(0);
            userDao.delete(account1);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    Life_UserInfo account1;

    public Life_UserInfo queryForone(String account) {
        try {
            account1 = userDao.queryBuilder().where().eq("account", account).query().get(0);


        } catch (Exception e) {
            e.printStackTrace();
        }
        return account1;

    }

    public void updateUser(String account, String password) {


        try {


            UpdateBuilder<Life_UserInfo, Integer> life_devicesIntegerUpdateBuilder = userDao.updateBuilder();
            life_devicesIntegerUpdateBuilder.updateColumnValue("password", password).where().eq("account", account);
            life_devicesIntegerUpdateBuilder.update();


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void updateUserper(String account, String permission) {


        try {


            UpdateBuilder<Life_UserInfo, Integer> life_devicesIntegerUpdateBuilder = userDao.updateBuilder();
            life_devicesIntegerUpdateBuilder.updateColumnValue("permission", permission).where().eq("account", account);
            life_devicesIntegerUpdateBuilder.update();


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public String map() {
        Map<String, Object> map = new HashMap<String, Object>();       //数据采用的哈希表结构
        //给map中添加元素
        map.put("录入VIN", false);
        map.put("录入OBU", false);
        map.put("蓝牙检测", false);
        map.put("OBU自检", false);
        map.put("激活", false);
        map.put("交易", false);
        String s = JSONUtils.mapToJson(map);
        Log.e("结果", s);
        return s;
    }
}
